<?php
require_once 'Zend/Loader.php' ;

define ( 'CTS_ENTRY' , '.' ) ;

class Bootstrap
{
    public static $frontController = null ;
    public static $root = '' ;
    public static $registry = null ;
    
    public static function run()
    {
        self::prepare() ;
        try
        {
            $response = self::$frontController->dispatch() ;
            self::sendResponse( $response ) ;
        }
        catch( Exception $e )
        {
            print_r ( '<pre>' ) ;
            print_r ( $e ) ;
            print_r ( '</pre>' ) ;
        }
    }
    
    public static function prepare()
    {
        //setup environment
        self::setupEnvironment() ;
        
        //setup register
        Zend_Loader::registerAutoload() ;
        self::setupRegistry() ;
        
        //setup configuration
        self::setupConfiguration() ;
        
        //setup Session
        Zend_Session::start( true ) ;
        
        //setup frontController
        self::setupFrontController() ;
        
        //setup database driver
        self::setupDatabase() ;
        
        //setup logger
        self::setupLogger() ;
        
        //setup system running local
        Zend_Locale::setDefault( 'zh_CN' ) ;
    }
    
    public static function setupEnvironment()
    {
        //setup error report level
        error_reporting( E_ALL & ~E_NOTICE ) ;
        ini_set( 'display_errors' , true ) ;
        
        //setup running root 
        self::$root = dirname(dirname(__FILE__));
        define('HTMLPURIFIER_PREFIX', self::$root . '/library');  
         
        //setup Time zone
        date_default_timezone_set( 'Asia/Shanghai' ) ;
        
        //setup include path
        set_include_path(
            CTS_ENTRY . PATH_SEPARATOR .
            CTS_ENTRY . '/library' . PATH_SEPARATOR .
            CTS_ENTRY . '/app' . PATH_SEPARATOR .
            get_include_path()
        ) ;
    }
    
    public static function setupRegistry()
    {
        self::$registry = new Zend_Registry( array() , ArrayObject::ARRAY_AS_PROPS ) ;
        Zend_Registry::setInstance( self::$registry ) ;
    }
    
    public static function setupConfiguration()
    {
        $config = new Zend_Config_Ini(
            self::$root . '/config/config.ini' ,
            'general'
        ) ;    
        Zend_Registry::set( 'config' , $config ) ;
        self::$registry->configuration = $config ;
    }
    
    public static function setupFrontController()
    {
        Zend_Loader::registerAutoload() ;
        self::$frontController = Zend_Controller_Front::getInstance() ;
        self::$frontController->registerPlugin( new Cts_Plugin_Allow ) ;
        self::$frontController->throwExceptions( true ) ;
        self::$frontController->returnResponse( true ) ;
        self::$frontController->addModuleDirectory( self::$root . '/modules' ) ;
        self::$frontController->setParam( 'registry' , self::$registry ) ;
        self::$frontController->setParam( 'noViewRenderer' , true ) ;       
    }
    
    public static function setupDatabase()
    {
        $config = self::$registry->configuration;
        $db = Zend_Db::factory($config->db->adapter, $config->db->toArray());
        $db->query("SET NAMES 'utf8'");
        self::$registry->db = $db;
        Zend_Db_Table::setDefaultAdapter($db);
    }
    
    public static function setupLogger()
    {
        $config = self::$registry->configuration ;
        $logger = new Cts_Log( $config ) ;
        $logger->setClass( 'Bootstrap' ) ;    
        $logger->info( 'Setup Logger OK!' , __FUNCTION__ ) ;
        self::$registry->logger = $logger ;
    }
    
    public static function sendResponse(Zend_Controller_Response_Http $response)
    {
        $response->setHeader('Content-Type', 'text/html; charset=utf-8', true);
        $response->sendResponse();
    }
}